package com.hangzhou.welbeing.welbeinginstrument.fragement;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.adapter.ReleaseHotAdapter;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.NewPostBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司14
 * Created by 20433 on 2018/2/1
 */

public class releaseHotFragment extends Fragment {
    @Bind(R.id.fragment_hot_xRecyclerView)
    XRecyclerView fragmentHotXRecyclerView;
    private Context mcontext;
    private int p = 1;
    private boolean flag = true;
    private ReleaseHotAdapter releaseHortAdapter;
    private String circle_id;
    private  List<NewPostBean.DataBean> mdateBean = new ArrayList<>();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mcontext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(mcontext, R.layout.fragment_hot, null);
        ButterKnife.bind(this, view);
        Intent intent = getActivity().getIntent();
        circle_id = intent.getStringExtra("circle_id");

        LinearLayoutManager layoutManager =   new LinearLayoutManager(mcontext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        fragmentHotXRecyclerView.setLayoutManager(layoutManager);
        fragmentHotXRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        fragmentHotXRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        fragmentHotXRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);
       fragmentHotXRecyclerView.setPullRefreshEnabled(false);  //TODO 禁止下拉刷新完毕
        fragmentHotXRecyclerView.getDefaultFootView().setNoMoreHint("就这么多了");
        fragmentHotXRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                flag = true;
                loadReleaseHotDate(p);
            }

            @Override
            public void onLoadMore() {
                flag = false;
                loadReleaseHotDate(p);
                p++;
            }
        });
        releaseHortAdapter = new ReleaseHotAdapter(mcontext); // TODO 传入数据
        fragmentHotXRecyclerView.setAdapter(releaseHortAdapter);
        fragmentHotXRecyclerView.refresh();
        releaseHortAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view) { // TODO 点击进入详情页

            }

            @Override
            public void onItemFirstButtonClick(View v, int position) {  //TODO 点击 跳转关注

            }
        });
        loadReleaseHotDate(p);
        return view;
    }
    /**
     * @desc 加载最热数据
     * */
    private void loadReleaseHotDate(int p) {
        SharedPreferences sp = mcontext.getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.NEW_HOSTS)
                .addParams("cat_id",circle_id)
                .addParams("cus_id", user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse = response.body().string();
                        Gson gson =  new Gson();
                        Log.e("tag","parseNetworkResponse===####====="+parseNetworkResponse);
                        final NewPostBean newPostBean =  gson.fromJson(parseNetworkResponse, NewPostBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(newPostBean.errorCode == 200){
                                    List<NewPostBean.DataBean> mlistdate = newPostBean.data;
                                    if(mlistdate != null && mlistdate.size() >0){
                                        mdateBean.addAll(mlistdate);
                                        if(flag){
                                            fragmentHotXRecyclerView.refreshComplete(); //TODO 下拉刷新完毕
                                        }else{
                                            fragmentHotXRecyclerView.loadMoreComplete();  //TODO 上拉加载完毕
                                        }
                                    }else {
                                        fragmentHotXRecyclerView.setNoMore(true);
                                    }
                                    releaseHortAdapter.notifyDataSetChanged();
                                }else{
                                    Toast.makeText(mcontext,newPostBean.errorMessage,Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(mcontext,"网络异常",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
